<script setup>
import { ref,onMounted } from 'vue'
import { queryWarehousingEntryDetalisSort,updateWarehousingEntryDetalisSort,updateWarehousingEntryDetalisSortByCount } from '../../api/warehousingentrydetailssort'
import { ElMessage, ElMessageBox } from 'element-plus'
//定义表格数据
const warehousingEntryDetailsSortList = ref([])
//定义表单数据
const warehousingEntryDetailsSortFrom = ref({supplierName:''})

//查询的点击事件
const query = async ()=>{ 
  const result = await queryWarehousingEntryDetalisSort(page.value,pageSize.value,warehousingEntryDetailsSortFrom.value.supplierName)
  warehousingEntryDetailsSortList.value = result.data.rows
  total.value = result.data.total
}
//清空表单数据
const clear = ()=>{
  warehousingEntryDetailsSortFrom.value = {supplierName:''}
  query()
}

//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({count:''})

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}

//挂载
onMounted(() => {
  query()
})

//确认分拣的点击事件
const confirmPicking = async (row)=>{ 
  //弹出一个确认框;
  ElMessageBox.confirm('确认分拣完成？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const result = await updateWarehousingEntryDetalisSort(row);
    if(result.code){
      ElMessage.success('操作成功')
      query()
    }else{
      ElMessage.error('操作失败！请重新操作')
    }
  }).catch(() => {
    // 用户点击取消后的操作
    ElMessage.info('已取消操作')
  })
}

//确定分拣的点击事件
const submitPicking = (id)=>{
  dialogData.value.count = ''
  dialogShow.value = true
  title.value = '分拣数量'
  countData.value.id = id
}

//定义分拣数量的数据
const countData = ref({id:'',count:''})

//确定点击事件
const confirmeSortInfo = async () => {
  countData.value.count = dialogData.value.count
  const result = await updateWarehousingEntryDetalisSortByCount(countData.value)
  if(result.code){
    ElMessage.success('操作成功')
    query()
    dialogShow.value = false
    countData.value = {id:'',count:''}
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}

</script>

<template>
<div><h4>待分拣</h4></div>

<div>
  <el-form :inline="true" :model="warehousingEntryDetailsSortFrom" class="demo-form-inline">
    <el-form-item>
      <el-input v-model="warehousingEntryDetailsSortFrom.supplierName" placeholder="供应商名称" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="warehousingEntryDetailsSortList" border style="width: 100%" >
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="batchNumber" label="批次号" align="center" width="120"/>
    <el-table-column prop="supplierName" label="供应商名称" align="center" width="180"/>
    <el-table-column prop="title" label="商品名称" align="center" width="200"/>
    <el-table-column prop="total" label="商品总数量" align="center" width="120"/>
    <el-table-column prop="price" label="商品价格" align="center" width="120"/>
    <el-table-column prop="consignorName" label="货主名称" align="center"  width="200"/>
    <el-table-column prop="count" label="分拣数量" align="center" width="120"/>
    <el-table-column fixed="right" label="操作" align="center" width="120">
      <template #default="scope">
        <el-button type="primary" size="small" @click="submitPicking(scope.row.id)" plain><el-icon><Plus /></el-icon></el-button>
        <el-button type="success" size="small" @click="confirmPicking(scope.row)" plain><el-icon><Select /></el-icon></el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>


<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="500" align-center>
    <el-form :model="dialogData">
      <el-form-item>
        <el-input v-model="dialogData.count" placeholder="输入分拣的数量" style="width: 350px;"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default">取消</el-button>
      <el-button type="primary" @click="confirmeSortInfo">确定</el-button>
    </template>
  </el-dialog>
</div>
</template>
<style scoped>
</style>